package com.twq.types.typeparam.covariant

/**
 * Created by tangweiqun on 2017/12/6.
 */
class Cell[T](init: T) {
  private[this] var current: T = init

  def get = current

  def set(x: T) = {
    current = x
  }
}

object Covariant {
  def main(args: Array[String]): Unit = {
    //1、协变使得运行时类型设置不对
    val c1 = new Cell[String]("abc")
    //val c2: Cell[Any] = c1 //假设Cell是可以协变的
    //c2.set(1) //因为1是Int类型，是Any类型的子类，将Int类型的值赋值给了String类型的Cell了，这样肯定不对
    val s: String = c1.get



    //2、Mutable classes must be invariant
  }
}
